package com.ka.magicexplorer.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ProgressBar;

import com.ka.magicexplorer.model.gif_file;
import com.ka.magicexplorer.model.jpg_file;
import com.ka.magicexplorer.model.png_file;
import com.ka.magicexplorer.provider.MySession;

public class MainActivity extends Activity {

	protected ProgressBar progressDialog;
	protected File rootFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();
		execute();
	}

	private void init() {
		progressDialog = (ProgressBar) findViewById(R.id.progressbar);
		rootFile = new File(Environment.getExternalStorageDirectory().getPath());
	}

	private void execute() {
		findFile();
	}

	private void findFile() {
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				try {
					listFile(rootFile);

					Intent itent = new Intent(MainActivity.this,
							ListFunctActivity.class);
					startActivity(itent);
					
					finish();

				} catch (Exception e) {

				}
			}
		};

		new Thread(runnable).start();
	}

	protected void listFile(File file) {

		if (file.isDirectory()) {
			File[] lstChildFile = file.listFiles();

			for (File f : lstChildFile) {
				listFile(f);
			}
		} else {
			String extension = "";
			
			int i = file.getName().lastIndexOf('.');
			int p = Math.max(file.getName().lastIndexOf('/'), file.getName()
					.lastIndexOf('\\'));

			if (i > p) {
				extension = file.getName().substring(i + 1);

				splitFile(file, extension);
			}

		}
	}

	private void splitFile(File file, String extension) {
		if (extension.equals("png")) {
			MySession.getInstance().getPngFileList().add(new png_file(file.getPath(), file.getName(),file.isHidden()));
		}
		else 
		if(extension.equals("jpg")){
			MySession.getInstance().getJpgFileList().add(new jpg_file(file.getPath(), file.getName(), file.isHidden()));
		}
		else
		if(extension.equals("gif")){
			MySession.getInstance().getGifFileList().add(new gif_file(file.getPath(), file.getName(), file.isHidden()));
		}
		else
		if(extension.equals("mp3")){
			
		}
		else
		if(extension.equals("mp4")){
			
		}
		else
		if(extension.equals("mkv")){
			
		}
		else
		if(extension.equals("flv")){
			
		}
	}

}
